/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.listener;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;

import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.editpart.IUMLObjectEditPart;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 * 
 * @author Patrick Tessier
 * @since 4 oct. 06
 * this class is used to create a listener on uml element and to call automatically editPart.refresh
 * when the uml element is modified
 */

public class UmlElementListenerForTree implements Adapter {

	/**
	 * 
	 */
	private Notifier _target;
	/**
	 * 
	 */
	protected EditPart umlEditPart;

	private static final int channel = 300;


	/**
	 * this class create a listener on the umlElement.
	 * 
	 * @param editPart the editpart of the UMLElement
	 */
	public UmlElementListenerForTree(IUMLObjectEditPart editPart){

		this.umlEditPart=(EditPart)editPart;

	}

	/**
	 * 
	 * 
	 * @param notification 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 */
	public void notifyChanged(Notification notification) {
		Debug.debug(this, ""+notification, channel);
		if (notification.getEventType() != Notification.REMOVING_ADAPTER
				&& notification.getEventType() != PapyrusNotification.ELEMENT_DELETED
				&& !((notification.getNotifier() instanceof Uml1SemanticModelBridge)&& notification.getEventType()==Notification.SET)) {
			// it seems the editpart can be woke up to soon!
			if( umlEditPart.getParent()!=null && umlEditPart.getRoot()!=null){
				umlEditPart.refresh();	
			}
			//FIXME : to delete when the modelmanager knows when uml model is modified.	
			if ( umlEditPart instanceof IUMLElementEditPart && notification.getEventType() != Notification.REMOVING_ADAPTER ) {
				((IUMLElementEditPart) umlEditPart).getModelManager().modifyUMLElement(((IUMLElementEditPart) umlEditPart).getUmlElement());
			}
			if ( (notification.getEventType() ==  Notification.SET) && (notification.getNotifier() instanceof Uml1SemanticModelBridge) ) {	
				if( umlEditPart instanceof GraphNodeEditPart){
					Debug.debug(this, "CHANGE FIGURE", channel);	
					((GraphNodeEditPart) umlEditPart).changeFigureToStereotypeFigure();
				}				
			}
		}
	}

	/**
	 * 
	 * 
	 * @return 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return this._target;
	}

	/**
	 * 
	 * 
	 * @param newTarget 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		this._target = newTarget;
	}

	/**
	 * 
	 * 
	 * @param type 
	 * 
	 * @return 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return type.equals(umlEditPart.getModel().getClass());
	}

}